package site.yunnong.atvris.portal.spider.haokan;

import com.geccocrawler.gecco.GeccoEngine;
import com.geccocrawler.gecco.request.HttpGetRequest;
import com.google.common.collect.Lists;
import org.springframework.context.annotation.Bean;
import site.yunnong.atvris.dao.domain.MediaPartition;
import site.yunnong.atvris.portal.spider.SpringGeccoEngine;
import site.yunnong.atvris.portal.spider.SpringPipelineFactory;
import site.yunnong.atvris.service.MediaPartitionService;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author zjh
 * @date 2021/8/30 0:03
 */
// 需要爬虫时再开启
//@Configuration
public class HaokanBeanConfig {

    @Bean
    public SpringPipelineFactory springPipelineFactory() {
        return new SpringPipelineFactory();
    }

    @Resource
    private MediaPartitionService mediaPartitionService;

    @Bean
    public SpringGeccoEngine initGecco() {
        List<MediaPartition> mediaPartitions = mediaPartitionService.querySinglePartition();
        return new SpringGeccoEngine() {
            @Override
            public void init() {
                List<HttpGetRequest> requests = Lists.newArrayList();
                for (MediaPartition mediaPartition : mediaPartitions) {
                    for (int i = 1; i < 11; i++) {
                        String url = "https://haokan.baidu.com/web/search/api?pn=" + i + "&rn=10&type=video&query=" + mediaPartition.getPartitionName();
                        HttpGetRequest start = new HttpGetRequest(url);
                        start.addParameter("pid", String.valueOf(mediaPartition.getId()));
                        start.addHeader("Referer", "https://haokan.baidu.com/web/search/page?query=" + mediaPartition.getPartitionName());
                        requests.add(start);
                    }
                }
                GeccoEngine.create()
                        .pipelineFactory(springPipelineFactory())
                        .classpath("site.yunnong.atvris.portal.spider.haokan")
                        .seed(requests)
                        .interval(2000)
                        .start();
//                String url = "https://haokan.baidu.com/web/search/api?pn=1&rn=10&type=video&query=林木育种";
//                HttpGetRequest start = new HttpGetRequest(url);
//                start.addParameter("pid", "58");
//                GeccoEngine.create()
//                        .pipelineFactory(springPipelineFactory())
//                        .classpath("site.yunnong.atvris.portal.spider.haokan")
//                        .start(start)
//                        .interval(2000)
//                        .start();
            }
        };
    }
}
